import 'package:auto_animated/auto_animated.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:enter_prise_web/common/constants.dart';
import 'package:enter_prise_web/models/group.dart';
import 'package:enter_prise_web/models/object.dart';
import 'package:enter_prise_web/page/index/index_controller.dart';
import 'package:enter_prise_web/page/settings/setting_controller.dart';
import 'package:enter_prise_web/util/MyDio.dart';
import 'package:enter_prise_web/widgets/generic_form.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';

class Settings extends StatefulWidget {
  const Settings({super.key});

  @override
  State<Settings> createState() => _SettingsState();
}

class _SettingsState extends State<Settings> {
  TextStyle style1 = const TextStyle(fontSize: 17, color: kTitleTextColor);
  final EasyRefreshController refreshController = EasyRefreshController(
      controlFinishRefresh: true, controlFinishLoad: true);
  TextStyle style2 = const TextStyle(fontSize: 17, color: kTextColor);
  final IndexController c = Get.put(IndexController());
  final myDio = MyDio();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
            decoration: BoxDecoration(borderRadius: BorderRadius.circular(10)),
            child: GetBuilder<settingController>(
                id: "setting_list",
                builder: (controller) {
                  var objects = controller.settings;
                  return EasyRefresh(
                    onRefresh: () async {
                      controller.refreshData();
                      refreshController.finishRefresh();
                    },
                    controller: refreshController,
                    child: Container(
                      padding: const EdgeInsets.only(
                          bottom: 10, top: 10, left: 10, right: 10),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(10),
                        color: kDefaualtBgColor,
                      ),
                      child: ListView.builder(
                          itemCount: objects.length,
                          itemBuilder: (context, index) {
                            var objectName = objects[index]["objectName"];
                            var value = objects[index]['value'];
                            var gid = objects[index]['gid'];
                            var commentary = objects[index]['commentary'];

                            return Card(
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  SizedBox(
                                    height: 60,
                                    child: ListTile(
                                      title: Text(
                                        objectName.toString(),
                                        style: const TextStyle(
                                            color: Colors.black),
                                      ),
                                      subtitle: Text(
                                          "组别: ${group.getGroupName(gid.toString()).toString()}"),
                                      trailing: Text("价值：$value 元"),
                                    ),
                                  ),
                                  Container(
                                      width: double.infinity,
                                      padding: const EdgeInsets.only(
                                          left: 16, bottom: 8.0),
                                      decoration: BoxDecoration(
                                          border: Border(
                                              bottom: BorderSide(
                                                  color: Colors.grey
                                                      .withOpacity(0.5)))),
                                      child: Text("备注：${commentary??"无"}")),
                                  SizedBox(
                                    width: double.infinity,
                                    height: 60,
                                    child: Row(
                                      mainAxisAlignment: MainAxisAlignment.end,
                                      children: [
                                        TextButton(
                                          onPressed: () {
                                            //弹出对话框询问用户是否删除该工件
                                            showCupertinoDialog(
                                                context: Get.context!,
                                                builder: (c) {
                                                  return CupertinoAlertDialog(
                                                      title: const Text("删除"),
                                                      content:
                                                          const Text("确定删除吗？"),
                                                      actions: [
                                                        CupertinoDialogAction(
                                                            child: const Text(
                                                                "取消"),
                                                            onPressed: () {
                                                              Get.back();
                                                            }),
                                                        CupertinoDialogAction(
                                                            child: const Text(
                                                                "确定"),
                                                            onPressed:
                                                                () async {
                                                              controller
                                                                  .deleteById(
                                                                      index);

                                                              Get.back();
                                                            })
                                                      ]);
                                                });
                                          },
                                          child: const Text("删除"),
                                        )
                                      ],
                                    ),
                                  )
                                ],
                              ),
                            );
                          }),
                    ),
                  );
                })));
  }

  LiveList liveList(settingController controller) {
    return LiveList(
      itemBuilder: (context, index, animation) {
        return FadeTransition(
            opacity: Tween<double>(begin: 0.0, end: 1.0).animate(animation),
            child: SlideTransition(
              position:
                  Tween<Offset>(begin: const Offset(0, 1), end: Offset.zero)
                      .animate(animation),
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Container(
                  clipBehavior: Clip.antiAlias,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(10),
                      color: Colors.white),
                  child: ExpansionTile(
                    initiallyExpanded: false,
                    title: Text(
                      controller.settings[index]['objectName'],
                      style: const TextStyle(
                          color: Colors.black,
                          fontSize: 18,
                          fontWeight: FontWeight.w400),
                    ),
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Row(
                                  children: [
                                    Text(
                                      "价格",
                                      style: style1,
                                    ),
                                    const SizedBox(
                                      width: 10,
                                    ),
                                    Text(
                                      "${controller.settings[index]['value']} 元",
                                      style: style2,
                                    )
                                  ],
                                ),
                                const SizedBox(
                                  height: 10,
                                ),
                                Row(
                                  children: [
                                    Text(
                                      "备注",
                                      style: style1,
                                    ),
                                    const SizedBox(
                                      width: 10,
                                    ),
                                    Text(
                                      controller.settings[index]
                                              ['commentary'] ??
                                          "",
                                      style: style2,
                                    ),
                                  ],
                                ),
                                const SizedBox(
                                  height: 10,
                                ),
                              ]),
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Row(
                                children: [
                                  Text(
                                    "分组",
                                    style: style1,
                                  ),
                                  const SizedBox(
                                    width: 10,
                                  ),
                                  Text(
                                    object
                                        .getGroupName(controller.settings[index]
                                                ['gid']
                                            .toString())
                                        .toString(),
                                    style: style2,
                                  )
                                ],
                              ),
                              const SizedBox(
                                height: 10,
                              ),
                              Row(
                                children: [
                                  Text(
                                    "编号",
                                    style: style1,
                                  ),
                                  const SizedBox(
                                    width: 10,
                                  ),
                                  Text(
                                    controller.settings[index]['oid']
                                        .toString(),
                                    style: style2,
                                  )
                                ],
                              ),
                              const SizedBox(
                                height: 10,
                              ),
                            ],
                          )
                        ],
                      )
                    ],
                  ),
                ),
              ),
            ));
      },
      itemCount: controller.settings.length,
    );
  }
}

class addButton extends StatelessWidget {
  const addButton({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return Positioned(
      right: 0,
      left: 0,
      bottom: 0,
      child: createBottomView(
          icons: const [Icons.add_outlined],
          bgcolor: Colors.white,
          clicks: [
            () {
              showCupertinoModalBottomSheet(
                  context: context,
                  builder: (context) {
                    return Container(
                      child: GenericForm(
                        labels: const ["工件名称", "工件编号", "组编号", "价格", "备注"],
                        onpress: (values) {
                          Get.back();
                        },
                      ),
                    );
                  });
            },
          ]),
    );
  }
}

createBottomView(
    {List<IconData>? icons,
    List<String>? labels,
    List<Function()>? clicks,
    Color? bgcolor}) {
  return Container(
    color: bgcolor ?? Colors.white,
    width: Get.width / icons!.length,
    height: 60,
    child: InkWell(
      splashColor: Colors.transparent,
      highlightColor: Colors.transparent,
      onTap: clicks![0],
      child: Icon(
        icons[0],
        color: kGrayColor,
        size: 40,
      ),
    ),
  );
}
